package com.ucvisitors.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.ucvisitors.dto.Building;


public class BuildingDbDAO extends SQLiteOpenHelper implements IBuildingDbDAO {

	
     private static final String ID ="Id";
     private static final String NAME = "Name";
     private static final String LATITUDE = "Latitude";
     private static final String LONGITUDE = "Longitude";
     private static final String BUILDINGS_TABLE = "buildings";
	 private String columns;
	
	public BuildingDbDAO (Context context)
	{
		super(context, "ucvisitors", null, 1);
		
		columns =  ID + ", " + NAME + ", " + LATITUDE + ", " + LONGITUDE;
	}
	
	@Override
	public List<Building> fetchBuildings() throws Exception{
		
		List<Building> allBuildings = new ArrayList<Building>();
		
		String sqlSelect = "select * from " + BUILDINGS_TABLE;

        Cursor cursor = getReadableDatabase().rawQuery(sqlSelect, null);
		
        if (cursor.getCount() > 0) {
            
            cursor.moveToFirst();
            
            while(!cursor.isAfterLast()) {
                    Building building = new Building();
                    
                    // populate the plant from the cursor.
                    createBuilding(building, cursor);
                    
                    // add the plant to the return collection.
                    allBuildings.add(building);
                    
                    // move to the next result.
                    cursor.moveToNext();
            }
    }
        
		return allBuildings;
	}

	private void createBuilding(Building building, Cursor cursor)
	{
		building.setId(cursor.getInt(cursor.getColumnIndex(ID)));
		building.setName(cursor.getString(1));
		building.setLon(cursor.getDouble(2));
		building.setLat(cursor.getDouble(3));
	}
	
	
	@Override
	public void insert(List<Building> allBuildings) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String schema = "CREATE TABLE " + BUILDINGS_TABLE + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " TEXT, " + LATITUDE + " DOUBLE, " + LONGITUDE + " DOUBLE);";
        schema.length();
        db.execSQL(schema);
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<String> fetchBuildingNames() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
}
